package com.sophie.dao.impl;

import com.sophie.dao.BaseDAO;
import com.sophie.dao.IUserDAO;
import com.sophie.entity.User;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: Rowena
 * @CreateTime: 2024-08-29 23:21:18
 * @Version: 1.0
 */

public class UserDAOImpl extends BaseDAO implements IUserDAO {
    @Override
    public int register(User user) throws SQLException {
        return super.insertReturnKey("insert into user(name,password,gender,birthday,education,address,hobby,likeType,avatar) " +
                "values(?,?,?,?,?,?,?,?,?)",user.getName(),user.getPassword(),user.getGender(),user.getBirthday(),
                user.getEducation(),user.getAddress(),user.getHobby(),user.getLikeType(),user.getAvatar());
    }

    @Override
    public User login(String name, String password) throws SQLException {
        return super.getOne("select * from user where name=? and password=?", User.class, name, password);
    }

    @Override
    public long checkUserName(String userName) throws SQLException {
        return super.getScalar("select count(*) from user where name=?",userName);
    }

    @Override
    public List<User> getUserList() throws SQLException {
        return super.getAll("select * from user ORDER BY RAND();", User.class);
    }

    @Override
    public User getUserById(int id) throws SQLException {
        return super.getOne("select * from user where id=?", User.class, id);
    }

    @Override
    public int updateUser(User user) throws SQLException {
        return super.update("update user set name=?,password=?,gender=?,birthday=?," +
                "education=?,address=?,hobby=?,avatar=?,likeType=?,motto=? where id=?",
                user.getName(),user.getPassword(),user.getGender(),user.getBirthday(),
                user.getEducation(),user.getAddress(),user.getHobby(),user.getAvatar(),user.getLikeType(),user.getMotto(),user.getId());
    }


}
